package com.lichuandi.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;


@RestController
@CrossOrigin(origins = "*") // 允许跨域请求
public class ImageController {
    /**
     *  模拟生成随机测试图片（类似 Python 的 np.random）
     * @return -- 返回生成的随机图片的字节流
     * @throws Exception -- 异常
     */
    @GetMapping(value = "/get_image2", produces = MediaType.IMAGE_PNG_VALUE)
    public byte[] getImage2() throws Exception {
        int width = 600, height = 400;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Random rand = new Random();

        // 生成随机 RGB 像素
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int r = rand.nextInt(256);
                int g = rand.nextInt(256);
                int b = rand.nextInt(256);
                image.setRGB(x, y, (r << 16) | (g << 8) | b);
            }
        }

        // 转换为 PNG 字节流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "png", baos);
        return baos.toByteArray();
    }
    @GetMapping(value = "/get_image", produces = MediaType.IMAGE_PNG_VALUE)
    public byte[] getImage() throws Exception {
        // 定义本地图片文件的路径
        String imagePath = "C:\\Users\\admin\\OneDrive\\图片\\屏幕快照\\捕获.png"; // 请将此路径替换为实际图片文件的路径
        File imageFile = new File(imagePath);

        // 检查文件是否存在
        if (!imageFile.exists()) {
            throw new Exception("图片文件不存在: " + imagePath);
        }

        // 读取图片文件
        BufferedImage image = ImageIO.read(imageFile);

        // 将图片转换为字节数组
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "png", baos);
        return baos.toByteArray();
    }

    /**
     *    处理剪切请求
     */

    @PostMapping("/process_crop1")
    public Map<String, Object> processCrop(@RequestBody Map<String, Object> requestData) {
        Map<String, Object> response = new HashMap<>();
        try {
            // 解析坐标和 Base64 图片
            int x = (int) requestData.get("x");
            int y = (int) requestData.get("y");
            int width = (int) requestData.get("width");
            int height = (int) requestData.get("height");
            String base64Image = (String) requestData.get("image");

            // 解码 Base64
            byte[] imageBytes = Base64.getDecoder().decode(base64Image);
            BufferedImage croppedImage = ImageIO.read(new ByteArrayInputStream(imageBytes));

            // 保存图片（示例路径）
            File outputFile = new File("cropped_image.png");
            ImageIO.write(croppedImage, "png", outputFile);

            response.put("status", "success");
            response.put("message", String.format("区域 (%d,%d) 尺寸 %dx%d 已保存", x, y, width, height));
        } catch (Exception e) {
            response.put("status", "error");
            response.put("message", e.getMessage());
        }
        return response;
    }
}
